package com.bingquan.company.http;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bingquan.company.http.domain.TrafficRespons;

import java.util.*;

public class HttpRequestExample {
    public static void main(String[] args) {
        String paasid_value = "8a7486be7ef7457c017f20b2e1b5003c";
        String passToken_value =
                "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJiZ19kYXRhX2J1cyIsImlhdCI6MTY0NTUyMTE1OSwic3lzdGVtSWQiOiI4YTc0ODZiZTdlZjc0NTdjMDE3ZjIwYjJlMWI1MDAzYyJ9.rFXx431D0bXhUulioOnb4Q_6XgE00Qc3NpdLlr-jiFE";
        String url = "https://deliver.digitalgd.com.cn/traffic/gateway/common/api";
        String type = "application/json";
        String nonce_value = UUID.randomUUID().toString();
        String timestamp_value = Long.toString(System.currentTimeMillis());
        String signature_value =
                SecureUtil.sha256(timestamp_value + passToken_value + nonce_value + timestamp_value).toUpperCase();


        // 发送短信
        String serviceId_value = "GDEM_14837115009585479701";
        Map<String, String> sendHeader = new HashMap<>();
        sendHeader.put("x-tsp-paasid", paasid_value);
        sendHeader.put("x-tsp-signature", signature_value);
        sendHeader.put("x-tsp-timestamp", timestamp_value);
        sendHeader.put("x-tsp-nonce", nonce_value);
        sendHeader.put("x-tsp-serviceid", serviceId_value);

        Map<String, Object> sendMap = new HashMap<>();
        sendMap.put("templateId", "10000104");
        List<String> content = new ArrayList<>();
        content.add("15608653631|6548");
        sendMap.put("content", content);

        /**
         * 使用huTool工具发送Http 消息， 使用alibaba fastJosn生成http body
         */
        HttpResponse sendResponse = HttpRequest.post(url).contentType(type).body(JSONObject.toJSONString(sendMap))
                .headerMap(sendHeader, Boolean.TRUE).execute();
        System.out.println(sendResponse.body());

        TrafficRespons send_trafficResonse = JSON.parseObject(sendResponse.body(), TrafficRespons.class);
        System.out.println(send_trafficResonse.toString());
    }
}
